﻿using System.Collections.Generic;
using System.ServiceProcess;
using Wicresoft.SMS.Common;
using System.ServiceModel;

namespace Wicresoft.SMS.Gateway.Host
{
    public partial class smsService : ServiceBase
    {
        private IEnumerable<Task> _tasklist;

        //private ServiceHost _host = new ServiceHost(typeof(SmsServiceSOAP));

        public smsService()
        {
            InitializeComponent();
            _tasklist = GlobalFactory.GetAllTasks();
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            Logger.Info(string.Format("Enter OnStart."));
            StartScan();
            Logger.Info(string.Format("_scanDB thread is starting"));

            //_host.Opened += delegate
            //{
            //    Logger.Info("SmsServiceSOAP is started.");
            //};
            //_host.Open();

        }

        protected override void OnStop()
        {
            base.OnStop();
            Logger.Info(string.Format("Enter OnStop."));
            AbortScan();
            Logger.Info(string.Format("_scanDB thread is abort"));
            //_host.Close();
            //Logger.Output(string.Format("WCF host is closed"));
        }

        protected override void OnPause()
        {
            base.OnPause();
            Logger.Info(string.Format("Enter OnPause."));
            

        }

        protected override void OnContinue()
        {
            base.OnContinue();
            Logger.Info(string.Format("Enter OnContinue."));
            foreach (var _task in _tasklist)
            {
                _task.Execute();
            }
        }

        private void StartScan()
        {
            Logger.Debug(string.Format("Enter StartScan."));
            foreach (var _task in _tasklist)
                _task.Execute();
        }

        /*
        private void PauseScan()
        {
            SMSScan.DisableScan();
            if ( _scanDB.ThreadState == System.Threading.ThreadState.Running ||  _scanDB.ThreadState == System.Threading.ThreadState.WaitSleepJoin )
            {
            }
        }

        private void ResumeScan()
        {
 
        }
         * */

        private void AbortScan()
        {
            foreach (var _task in _tasklist)
            {
                _task.AbortScan();
            }
        }
    }
}
